package com.demo.controller;


import com.demo.entity.Oil;
import com.demo.entity.Speed;
import com.demo.entity.Test;
import com.demo.service.OilService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.plaf.SplitPaneUI;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.*;

@CrossOrigin(origins = "*")
@Controller
public class OilController {
    @Autowired
    private OilService oilService;


    @PostMapping("/api/plane/upload")
    @ResponseBody
    public HashMap<String, Object> uploadFile(MultipartFile file) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "done");
        return map;
    }



    /**
     * 查询返回所有油田编号（oil_field），每个油田编号只返回一条。
     */
    @GetMapping("oil/getlist")
    @ResponseBody
    public HashMap<String, Object> getOilFieldListWithLocation() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            ArrayList<Map<String, Object>> list = oilService.getOilFieldListWithLocation();
            if (list != null && !list.isEmpty()) {
                map.put("code", 200);
                map.put("data", list);
                map.put("msg", "Success");
//                map.put("data", list);
                System.out.println(map);
            } else {
                map.put("code", 300);
                map.put("msg", "Failed");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 800);
            map.put("msg", "Try-catch error");
        }
        return map;
    }

    /**
     * 根据指定的油田编号和节点编号，返回指定节点的最新数据。
     */
    @PostMapping("oil/latestinfo")
    @ResponseBody
//    HashMap<String, Object>
    public HashMap<String, Object> getOilLatestInfo(@RequestBody Test test) {
//        System.out.println(test.getOil_field() + " " + test.getEui());
        HashMap<String, Object> map = new HashMap<>();
        try {
            Oil oil = oilService.getLatestInfo(test.getOil_field(), test.getEui());
            if (oil != null) {
                map.put("status", 200);
                map.put("msg", "OK");
                map.put("data", oil);
//                System.out.println(map.getClass());
            } else {
                map.put("code", 300);
                map.put("msg", "failed");
                map.put("data", null);
            }
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }


    /**
     * 根据根据指定的油田编号，返回该由田中所有节点的最新数据
     *
     * @return
     */
    @PostMapping("oil/latestdata/oilfeild")
    @ResponseBody
    public HashMap<String, Object> getLatestDataByOilField(@RequestBody Test test) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<Oil> list = oilService.getLatestDataByOilField(test.getOil_field());
            if (list != null) {
                map.put("code", 200);
                map.put("msg", "Success");
                map.put("data", list);
                System.out.println(list);
            } else {
                map.put("code", 300);
                map.put("msg", "failed");
                map.put("data", null);
            }
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    /**
     * 根据指定的油田编号和节点编号，返回所有数据。
     *
     * @return
     */
    @PostMapping("oil/data/fieldandnode")
    @ResponseBody
    public HashMap<String, Object> getDataByOilFieldAndNode(@RequestBody Test test) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<Oil> list = oilService.getDataByOilFieldAndNode(test.getOil_field(), test.getEui());
            if (list != null) {
                map.put("code", 200);
                map.put("msg", "Success");
                map.put("data", list);
            } else {
                map.put("code", 300);
                map.put("msg", "failed");
                map.put("data", null);
            }
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    /**
     * 根据指定油厂编号与 节点 时间 WiFi状态插入一条语句
     * @param oil_field
     * @param eui
     * @param wifi_status
     * @return
     */

//    @PostMapping("/insertWiFiStatusByFieldAndNode")
//    @ResponseBody
//    public HashMap<String, Object> insertWiFiStatusByFieldAndNode(String oil_field, String eui, String time,boolean wifi_status){
//        HashMap<String, Object> map = new HashMap<>();
//        try{
//           String str = oilService.insertWiFiStatusByFieldAndNode(oil_field, eui, time, wifi_status);
//            map.put("code", 200);
//            map.put("msg", str);
//        }catch (Exception e) {
//            System.out.println(Arrays.toString(e.getStackTrace()));
//            map.put("code", 800);
//            map.put("msg", "try-catch error");
//        }
//        return map;
//    }

    /***
     * 根据指定油厂编号与 节点更改WiFi状态wifi_status（默认值为false）
     * @return
     */
    @PostMapping("/updateWiFiStatusByFieldAndNode")
    @ResponseBody
    public HashMap<String, Object> updateWiFiStatusByFieldAndNode(@RequestBody Test test) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            oilService.updateWiFiStatusByFieldAndNode(test.getOil_field(), test.getEui(), test.isWifi_status());
            map.put("code", 200);
            map.put("msg", "Success");
            if (test.isWifi_status() == true) map.put("wifi_status", "=1 开启");
            else map.put("wifi_status", "=0 关闭");
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    /**
     * 根据指定油厂编号与 节点查询节点状态node_fault（默认值为false）
     *
     * @return
     */
    @PostMapping("/getNodeFaultByFieldAndNode")
    @ResponseBody
    public HashMap<String, Object> getNodeFaultByFieldAndNode(@RequestBody Test test) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int fault = oilService.getNodeFaultByFieldAndNode(test.getOil_field(), test.getEui());
            System.out.println(fault);
            map.put("code", 200);
            map.put("msg", "Success");
            if (fault == 1) map.put("node_fault", "=1 正常");
            else map.put("node_fault", "=0 异常");
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    /**
     * 指定油厂编号 or 节点id or 节点状态。返回所有数据 //返回800
     *
     * @return
     */

    @PostMapping("/getLatestDataByFieldAndNodeAndFault")
    @ResponseBody
    public HashMap<String, Object> getLatestDataByFieldAndNodeAndFault(@RequestBody Test test) {
        HashMap<String, Object> map = new HashMap<>();
        System.out.println(test.getNode_status());
        System.out.println(test.getEui());
        System.out.println(test.getOil_field());
        try (FileWriter fileWriter = new FileWriter("logging2.txt")){
            List<Oil> list = oilService.getLatestDataByFieldAndNodeAndFault(test.getOil_field(), test.getEui(), test.getNode_status());
            map.put("code", 200);
            map.put("msg", "Success");
            map.put("data", list);
            fileWriter.write(list.get(0).toString());
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }


    /**
     * 返回油厂数量
     *
     * @return
     */
    @PostMapping("/countOilFields")
    @ResponseBody
    public HashMap<String, Object> countOilFields() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int count = oilService.countOilFields();
            map.put("code", 200);
            map.put("msg", "Success");
            map.put("count", count);
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    /**
     * 返回所有节点总数（注意由于更新时间设计）分油厂给数据
     *
     * @return
     */
    @PostMapping("/getNodeCountByOilField")
    @ResponseBody
    public HashMap<String, Object> getNodeCountByOilField() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<Map<String, Object>> list = oilService.getNodeCountByOilField();
            int count = oilService.counteuis();
            map.put("code", 200);
            map.put("msg", "Success");
            map.put("count", count);
            map.put("list", list);
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    /**
     * 返回正常节点总数量，返回异常节点总数量（节点总数 = 正常节点 + 异常节点），均为最新时间
     *
     * @return
     */

    @PostMapping("/getNodeCountsByFault")
    @ResponseBody
    public HashMap<String, Object> getNodeCountsByFault() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<Map<String, Object>> list = oilService.getNodeCountsByFault();
            int normalcount = oilService.getTotalNormalNodeCount();
            int faultcount = oilService.getTotalFaultNodeCount();
            map.put("code", 200);
            map.put("msg", "Success");
            map.put("normalcount", normalcount);
            map.put("faultcount", faultcount);
            map.put("list", list);
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

    @PostMapping("/getFaultInfo")
    @ResponseBody
    public HashMap<String, Object> getFaultInfo(@RequestBody Test test) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<Oil> list = oilService.getFaultInfo(test.getOil_field(), false);
            map.put("code", 200);
            map.put("msg", "Success");
            map.put("list", list);
        } catch (Exception e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
            map.put("code", 800);
            map.put("msg", "try-catch error");
        }
        return map;
    }

}
